import { Key, PcCookie } from '@/utils/cookie'

import { getUserMenuList } from '@/api/user';


const state = {
    init: false, // 是否已加载用户菜单
    menuList: [], // 用户拥有的菜单集合
    buttonList: [] // 拥有按钮权限集合
}

const actions = {
    // 获取用户菜单
    getUserMenu({ commit }) {
        return new Promise((reslove, reject) => {
            const userId = PcCookie.get(Key.userInfoKey) ? JSON.parse(PcCookie.get(Key.userInfoKey)).uid : null
            console.log(userId);
            if (userId) {
                getUserMenuList(userId).then(response => {
                    console.log(response);
                    commit('SET_SYSTEM_MENU', response.data)
                    reslove()
                }).catch(error => {
                    reject(error)
                })
            }

        })

    }
}


const mutations = {
    SET_SYSTEM_MENU(state, data) {
        state.init = true
        state.buttonList = data.buttonList
        state.menuList = data.menuTreeList
    }
}

export default {
    namespaced: true, // 引用时需要模块名，即当前文件名 menu/getUserMenu
    state,
    actions,
    mutations
}